﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using NUnit.Framework;
using PropertyExpression.Common;
using PropertyExpression.ControllableQuery.Test.Queries;

namespace PropertyExpression.ControllableQuery.Test
{
    [TestFixture]
    public class MainTest
    {
        [Test]
        public void Test()
        {
            ExecutePartLinkQuery(
                queryContext => new ModelPublicationSearchQuery(
                                  queryContext.CreateParam("PublicationCode1").AsOptionalValue(),
                                  OptionalValue.Nothing<IParam<string>>(),
                                  OptionalValue.Nothing<IParam<string>>()
                                  )
                );
            Console.WriteLine();
            ExecutePartLinkQuery(
                queryContext => new ModelPublicationSearchQuery(
                                    queryContext.CreateParam("PublicationCode1").AsOptionalValue(),
                                    queryContext.CreateParam("ModelCode1").AsOptionalValue(),
                                    queryContext.CreateParam("ModelSequence1").AsOptionalValue()
                                    )
                );
            Console.WriteLine();
            ExecutePartLinkQuery(
                queryContext => new ModelPublicationSearchQuery(
                                  OptionalValue.Nothing<IParam<string>>(),
                                  OptionalValue.Nothing<IParam<string>>(),
                                  OptionalValue.Nothing<IParam<string>>()
                                  )
                );
        }

        private static void ExecutePartLinkQuery(Func<QueryContext, IQuery<IModelPublicationInfo>> queryFunc)
        {
            foreach (var dataReader in GetQueryEnumerable(queryFunc))
            {
                Console.WriteLine(
                    "PublicationId = '{0}', ModelCode = '{1}', ModelSequence = '{2}'",
                    dataReader.GetValue(_ => _.PublicationId),
                    dataReader.GetValue(_ => _.ModelCode),
                    dataReader.GetValue(_ => _.ModelSequence)
                    );
            }
        }

        private static IEnumerable<IDataReader<T>> GetQueryEnumerable<T>(
            Func<QueryContext, IQuery<T>> queryFunc)
        {
            using (var connection = new SqlConnection(
                @"Data Source=ws026\MSSQLSERVER2008;Initial Catalog=ControllableQuery;Integrated Security=True"))
            {
                connection.Open();

                using (var sqlCommand = new SqlCommand())
                {
                    sqlCommand.Connection = connection;

                    var query = queryFunc(new QueryContext(sqlCommand));

                    sqlCommand.CommandText = query.TransformText();

                    using (var sqlDataReader = sqlCommand.ExecuteReader())
                    {
                        while (sqlDataReader.Read())
                        {
                            yield return query.GetReader(sqlDataReader);
                        }
                    }
                }
            }
        }
    }
}